package com.wzdigit.wms.common.utils;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Administrator
 *
 */
@Slf4j
public class EasyExcelUtil {
	
	/**
	 * 导出excel核心方法一
	 * 
	 * @param response
	 * @param head
	 * @param data
	 * @param sheetName
	 * @throws Exception
	 */
	public static void export(HttpServletResponse response, Class<?> head, List<?> data, String sheetName) throws Exception {
		// 给定导出实体类
		EasyExcel.write(response.getOutputStream(), head)
				// 给定工作表名称
				.sheet(sheetName)
				// 给定样式
				.registerWriteHandler(getStyleStrategy())
				// 给定导出数据
				.doWrite(data);
	}

	/**
	 * 导出excel核心方法二
	 * 
	 * @param response
	 * @param head
	 * @param data
	 * @param sheetName
	 * @param horizontalCellStyleStrategy
	 * @throws Exception
	 */
	public static void export(HttpServletResponse response, Class<?> head, List<?> data, String sheetName,
			HorizontalCellStyleStrategy horizontalCellStyleStrategy) throws Exception {
		// 给定导出实体类
		EasyExcel.write(response.getOutputStream(), head)
				// 给定工作表名称
				.sheet(sheetName)
				// 给定样式
				.registerWriteHandler(horizontalCellStyleStrategy)
				// 给定导出数据
				.doWrite(data);
	}

	/**
	 * 设置请求头
	 * 
	 * @param request
	 * @param response
	 * @param fileName
	 * @throws Exception
	 */
	public static void setResponse(HttpServletRequest request, HttpServletResponse response, String fileName) throws Exception {
		String userAgent = request.getHeader("USER-AGENT");
        if (userAgent.contains("Mozilla")) {
            fileName = new String(fileName.getBytes(), "ISO8859-1");
        } else {
            fileName = URLEncoder.encode(fileName, "utf8");
        }
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-Type", "application/vnd.ms-excel");
		response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xlsx");
	}

	/**
	 * 样式
	 * 
	 * @return
	 */
	public static HorizontalCellStyleStrategy getStyleStrategy() {
		// 内容样式策略
		WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
		// 垂直居中,水平居中
		contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
		contentWriteCellStyle.setBorderLeft(BorderStyle.NONE);
		contentWriteCellStyle.setBorderTop(BorderStyle.NONE);
		contentWriteCellStyle.setBorderRight(BorderStyle.NONE);
		contentWriteCellStyle.setBorderBottom(BorderStyle.NONE);
		// 设置 自动换行
		// contentWriteCellStyle.setWrapped(true);
		// 字体策略
		WriteFont contentWriteFont = new WriteFont();
		// 字体大小
		contentWriteFont.setFontHeightInPoints((short) 10);
		contentWriteCellStyle.setWriteFont(contentWriteFont);
		
		// 头策略使用默认
		WriteCellStyle headWriteCellStyle = new WriteCellStyle();
		headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
		headWriteCellStyle.setWriteFont(contentWriteFont);
		return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
	}
}